package com.comsoftstar.nt5s.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hzc.nt5s.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


/**
 * Created by Administrator on 2017/6/21.
 */

public class ReportDataAdapter extends BaseAdapter {
    private JSONArray jsonArray;
    private Context mContext;

    public ReportDataAdapter(Context context, JSONArray ja) {
        this.jsonArray = ja;
        this.mContext = context;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {

        return jsonArray.length();
    }

    @Override
    public Object getItem(int position) {
        try {
            return jsonArray.get(position);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        JSONObject jo = new JSONObject();
        try {
            jo = (JSONObject) jsonArray.get(position);
        } catch (JSONException e) {

        }
        if (convertView==null){
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.report_item, null);
            viewHolder.group= (TextView) convertView.findViewById(R.id.report_item_group);
            viewHolder.total= (TextView) convertView.findViewById(R.id.report_item_total);
            viewHolder.totalN= (TextView) convertView.findViewById(R.id.report_item_totalN);
            viewHolder.totalQ= (TextView) convertView.findViewById(R.id.report_item_totalQ);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.group.setText(jo.optString("DeptName"));
        viewHolder.total.setText(jo.optString("Total"));
        viewHolder.totalQ.setText(jo.optString("TotalQ"));
        viewHolder.totalN.setText(jo.optString("TotalN"));
        return convertView;
    }
    class ViewHolder {
        TextView group, total, totalQ, totalN;
    }

}
